package com.example.uit_management;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint.Join;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Activity_list_pattern extends Activity {
	private Context context;
	private JsonParser_pattern jsonparser_pattern=new JsonParser_pattern();
	private JsonParser_class jsonparser_class=new JsonParser_class();
	private ListView list;	
	private ArrayAdapter_Pattern adapter;
	private ArrayList<HashMap<String, String>> array_jsonparser_class=new ArrayList<HashMap<String, String>>();
	private ArrayList<HashMap<String, String>> array_jsonparser_pattern=new ArrayList<HashMap<String, String>>();
	//private HashMap<String, String> jsonparser_pattern=new HashMap<String, String>();
	//private ArrayList<HashMap<String, String>> array_MAP=new ArrayList<HashMap<String, String>>();
	////////////////
	////khai bao bien cho spinner khoa
	/** spinner */
	private Spinner spinner_khoa;	
	/** Mang nien khoa hoc thi */
	ArrayList<String> ArrayList_khoa ;
	private ArrayAdapter spinnerArrayAdapter_khoa;
	/** Nien khoa **/
	private static String Spinner_semester_khoa="--Chọn khoa--";
	////////////////
	////khai bao bien cho spinner lop
	/** spinner */
	private Spinner spinner_lop;	
	/** Mang nien khoa hoc thi */
	ArrayList<String> ArrayList_lop ;
	private ArrayAdapter spinnerArrayAdapter_lop;
	/** Nien khoa **/
	private static String Spinner_semester_lop="--Chọn lớp--";
	    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_pattern);
        Menu_GridView.dialog.dismiss();
        Log.e(Activity_list_pattern.class.toString(), "Vao pattern ");
        
        try{
        	context = this;
        	array_jsonparser_pattern = jsonparser_pattern.getMap();
        	list=(ListView)findViewById(R.id.list_pattern);
        // call constructor method of adapter   
        // xap xep theo nam hoc   	
               
        //////////////////////////////////
        // tao spinner chon hoc khoa
        array_jsonparser_class = jsonparser_class.getMap();
        Collections.sort(array_jsonparser_class, new ArrayMap_class()); 
        ArrayList_khoa = new ArrayList<String>();
        ArrayList_khoa.clear();
        ArrayList_khoa.add("--Chọn Khoa--");
        HashMap<String, String> json_current;
        HashMap<String, String> json_before;
        for (int i = 0; i < array_jsonparser_class.size(); i++) {
        	if(i == 0){
        		json_current = array_jsonparser_class.get(i);
        		ArrayList_khoa.add("Khoa :"+json_current.get(JsonParser_class.KEY_facultyName)+"_"+json_current.get(JsonParser_class.KEY_facultyID));
        	}else{    
	        	json_current =array_jsonparser_class.get(i);
	        	json_before =array_jsonparser_class.get(i-1);
	        	if(!json_current.get(JsonParser_class.KEY_facultyID).equals(json_before.get(JsonParser_class.KEY_facultyID))){
	        		ArrayList_khoa.add("Khoa :"+json_current.get(JsonParser_class.KEY_facultyName)+"_"+json_current.get(JsonParser_class.KEY_facultyID));
        		}
        	}			
		}
		/////////////////////////////
		// Spriner
		///////////////////////////
		// set semetester for spinner
		//Spinner_semester_khoa="Tất cả";
		spinner_khoa = (Spinner) findViewById(R.id.spinner_pattern_Khoa);
		spinnerArrayAdapter_khoa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,ArrayList_khoa);
		spinner_khoa.setAdapter(spinnerArrayAdapter_khoa);			
		//////////////////////////////////
		// tao spinner chon hoc lop
		ArrayList_lop = new ArrayList<String>();
		ArrayList_lop.clear();
		ArrayList_lop.add("--Chọn lớp--");		
		/////////////////////////////
		// Spriner lop
		///////////////////////////
		// set semetester for spinner
		//Spinner_semester_khoa="Tất cả";
		spinner_lop = (Spinner) findViewById(R.id.spinner_pattern_Lop);
		spinnerArrayAdapter_lop = new ArrayAdapter(this,android.R.layout.simple_spinner_item,ArrayList_lop);
		spinner_lop.setAdapter(spinnerArrayAdapter_lop);		
		//spinner_lop.setOnItemSelectedListener(Spinner_ItemSelectedListener_Lop);
		spinner_lop.setSelection(0);
		spinner_lop.setEnabled(false);	
		//////////////////////////////
		// set lang nghe sellectItem
		spinner_khoa.setOnItemSelectedListener(Spinner_ItemSelectedListener_Khoa);
		spinner_khoa.setSelection(0);
		//////////////////////////////
		// Set gia tri cho list view
		Collections.sort(array_jsonparser_pattern, new ArrayMap_pattern());
		Log.e(Activity_list_pattern.class.toString(), "============================");
		for (int i = 0; i < array_jsonparser_pattern.size(); i++) {
			Log.e(Activity_list_pattern.class.toString(),"Lop "+array_jsonparser_pattern.get(i).get(JsonParser_pattern.KEY_classID)+ "Hoc ky " + array_jsonparser_pattern.get(i).get(JsonParser_pattern.KEY_patternSemester));
		}
		//adapter =new ArrayAdapter_Pattern(this, array_jsonparser_pattern);
        // add adapter into list
        if(array_jsonparser_pattern.size()==0){
        	Toast.makeText(this, "Không có môn nào", Toast.LENGTH_LONG).show();
        }
        //list.setAdapter(adapter);     
        }catch(Exception e){
        	e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity__score, menu);
        return true;
    }  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_back:{
        	finish();  
        	Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
				startActivity(intent);				
				break;
        	}
        }
        return true;
    }   
    static public String getSemester_Spinner_khoa(){
    	return Spinner_semester_khoa;
    }
    static public String getSemester_Spinner_lop(){
    	return Spinner_semester_lop;
    }
    private OnItemSelectedListener Spinner_ItemSelectedListener_Khoa = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        	// khi thay doi staue   
        	ArrayList_lop.clear();        		
    		ArrayList_lop.add("--Chọn lớp--");    	
    		spinner_lop.setEnabled(true);
    		spinner_lop.setSelection(0);
            Spinner_semester_khoa=spinner_khoa.getSelectedItem().toString();
        	Log.e(Activity_list_pattern.class.toString(),"SPinner chose select :" +spinner_khoa.getSelectedItem().toString());
        	if(Spinner_semester_khoa.equals("--Chọn Khoa--")){
        		ArrayList_lop.clear();
        		ArrayList_lop.add("--Chọn lớp--");	
        		spinner_lop.setSelection(0);
        		spinner_lop.setEnabled(false);
        	}else{        		
        		for (int i = 0; i < array_jsonparser_class.size(); i++) {
                	if(array_jsonparser_class.get(i).get(JsonParser_class.KEY_facultyID).equals(Spinner_semester_khoa.split("_")[Spinner_semester_khoa.split("_").length-1])){                		
                		ArrayList_lop.add("Lớp :"+array_jsonparser_class.get(i).get(JsonParser_class.KEY_classID));
                	}
        		}        		
        	}
        	spinner_lop = (Spinner) findViewById(R.id.spinner_pattern_Lop);        	
        	spinnerArrayAdapter_lop = new ArrayAdapter(context,android.R.layout.simple_spinner_item,ArrayList_lop);
    		spinner_lop.setAdapter(spinnerArrayAdapter_lop);		
    		spinner_lop.setOnItemSelectedListener(Spinner_ItemSelectedListener_Lop);
    		spinner_lop.setSelection(0);        	
        	
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    };
    private OnItemSelectedListener Spinner_ItemSelectedListener_Lop = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        	// khi thay doi staue           	
            Spinner_semester_lop=spinner_lop.getSelectedItem().toString();
        	Log.e(Activity_list_pattern.class.toString(),"SPinner chose select :" +spinner_lop.getSelectedItem().toString());
        	if(Spinner_semester_lop.equals("--Chọn lớp--")){ 
            	Log.e(Activity_list_pattern.class.toString(),"1");
        		SetAdapter();
        	}else{     
        		Log.e(Activity_list_pattern.class.toString(),"2");
        		SetAdapter_Filter();    		
        	}    
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    };
    private void SetAdapter(){
    	// Get Id of list
    	Log.e(Activity_list_pattern.class.toString(),"3");
    	ArrayList<HashMap<String, String>> array_Map_filter=new ArrayList<HashMap<String,String>>();
    	array_Map_filter.clear();
    	list=(ListView)findViewById(R.id.list_pattern);  
    	adapter =new ArrayAdapter_Pattern(this, array_Map_filter);
    	list.setAdapter(adapter);
    	
    }
    private void SetAdapter_Filter(){
    	// Get Id of list
    	Log.e(Activity_list_pattern.class.toString(),"4");
    	ArrayList<HashMap<String, String>> array_Map_filter=new ArrayList<HashMap<String, String>>();
    	HashMap<String, String> Map;
    	for (int i = 0; i < array_jsonparser_pattern.size(); i++) {
    		Map=array_jsonparser_pattern.get(i);
    		if(getSemester_Spinner_lop().split("\\:")[1].equals(Map.get(JsonParser_class.KEY_classID))){
    			array_Map_filter.add(Map);
        	}
		}    
    	Log.e(Activity_list_pattern.class.toString(),"+++");
		for (int i = 0; i < array_Map_filter.size(); i++) {
			Log.e(Activity_list_pattern.class.toString(),"Lop "+array_Map_filter.get(i).get(JsonParser_pattern.KEY_classID)+ "Hoc ky " + array_Map_filter.get(i).get(JsonParser_pattern.KEY_patternSemester));
		}
    	list=(ListView)findViewById(R.id.list_pattern);
    	adapter =new ArrayAdapter_Pattern(this, array_Map_filter);
    	list.setAdapter(adapter);
    }
    
}

